<template>
  <el-row>
    <el-card>
      <el-col :span="24">
        <div class="table_content" style="margin-bottom: 1px">
          <el-row :gutter="4">
            <el-form
              :inline="true"
              label-position="left"
              class="demo-form-inline"
              label-width="100px"
            >
              <el-form-item>
                <el-input
                  v-model="searchDto.keywords"
                  placeholder="请输入关键字"
                  style="width:250px"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button icon="el-icon-search" type="primary" @click="search">
                  查询
                </el-button>
              </el-form-item>
              <el-form-item>
                <el-button icon="el-icon-s-open" type="primary" @click="clear">
                  重置
                </el-button>
              </el-form-item>
              <el-form-item class="clear_item-margin-bottom">
                <el-button
                  type="primary"
                  icon="el-icon-circle-plus"
                  @click="newColumn()"
                  >新增栏目
                </el-button>
              </el-form-item>
            </el-form>
          </el-row>
        </div>
        <div class="table_content">
          <page-table
            ref="multipleSelection"
            :page-data="columnTree"
            :loading="loading"
            :do-get-page-data="getPageData"
          >
            <el-table-column
              label="序号"
              align="center"
              type="index"
              width="100px"
            ></el-table-column>
            <el-table-column
              prop="name"
              label="栏目名称"
              align="center"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="createTime"
              label="创建时间"
              align="center"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="sort"
              label="排序"
              align="center"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              label="管理"
              width="200"
              align="center"
              fixed="right"
            >
              <template slot-scope="scope">
                <el-tooltip content="编辑" placement="top">
                  <el-button
                    type="button"
                    size="mini"
                    icon="el-icon-edit"
                    @click="editColumn(scope.row)"
                  >
                  </el-button>
                </el-tooltip>
                <el-tooltip content="删除" placement="top">
                  <el-button
                    size="mini"
                    icon="el-icon-delete"
                    @click="delColumn(scope.row)"
                  >
                  </el-button>
                </el-tooltip>
              </template>
            </el-table-column>
          </page-table>
          <el-dialog
            :visible.sync="showColumnDialog"
            title="新增/编辑栏目:"
            width="600px"
            @close="fail"
          >
            <el-form
              ref="form"
              :model="formInfo"
              :rules="rules"
              :gutter="10"
              label-width="100px"
            >
              <el-form-item label="栏目名称：" prop="name">
                <el-input
                  placeholder="请输入栏目名称"
                  v-model="formInfo.name"
                  class="si"
                ></el-input>
              </el-form-item>
              <el-form-item label="排序：" prop="sort">
                <el-input-number
                  v-model="formInfo.sort"
                  :min="0"
                  label="描述文字"
                ></el-input-number>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="saveColumn">
                保存
              </el-button>
              <el-button @click="fail">
                取消
              </el-button>
            </div>
          </el-dialog>
        </div>
      </el-col>
    </el-card>
  </el-row>
</template>
<script>
import { mapGetters } from "vuex";
import _ from "lodash";
export default {
  name: "column",
  created() {
    this.getPageData();
  },
  mounted() {},
  data() {
    return {
      loading: false,
      searchDto: {
        pageSize: 10,
        pageIndex: "",
        keywords: ""
      },
      showColumnDialog: false,
      rules: {
        name: [{ required: true, message: "请填写名称", trigger: "blur" }],
        sort: [{ required: true, message: "请填写栏目排序", trigger: "blur" }]
      },
      formInfo: {
        id: "",
        name: "",
        sort: ""
      }
    };
  },
  computed: {
    ...mapGetters({
      columnTree: "columnTree"
    })
  },
  methods: {
    // 删除栏目
    delColumn(row) {
      this.$confirm("确认删除当前栏目?", "提示", {})
        .then(() => {
          let tem = {
            id: row.id
          };
          this.$store.dispatch("columnDelUsingPOSTS", tem).then(ret => {
            this.$message({
              message: "删除成功",
              type: "success"
            });
            this.getPageData();
          });
        })
        .catch(() => {});
    },
    // 添加栏目
    newColumn() {
      this.$store.commit("INIT_CURRENT_DICT");
      this.showColumnDialog = true;
    },
    // 获取分页数据
    getPageData(val) {
      let tem = Object.assign(this.searchDto, val);
      this.$store.dispatch("columnTreeUsingGETS", tem).then(ret => {
        console.log("PageData", ret.data);
      });
    },
    // 新增/编辑栏目
    saveColumn() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$store
            .dispatch("columnEditUsingPOSTS", { body: this.formInfo })
            .then(() => {
              this.$message({
                message: "操作成功",
                type: "success"
              });
              this.getPageData();
              this.showColumnDialog = false;
            });
        } else {
          return false;
        }
      });
    },
    //编辑
    editColumn(row) {
      this.formInfo = _.clone(row);
      this.showColumnDialog = true;
    },
    // 查询
    search() {
      this.getPageData(1);
    },
    //清空
    clear() {
      this.searchDto = {
        pageSize: 10,
        keywords: ""
      };
      this.getPageData(1);
    },
    //关闭弹窗
    fail() {
      this.formInfo = {
        id: "",
        name: "",
        sort: ""
      };
      this.showColumnDialog = false;
    }
  }
};
</script>
<style scoped></style>
